﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *  Copyright (C) 2010 by Barbara Post postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeWebUI;
using OfficeWebUI.Ribbon;
using OsamesFrameworkCore;
using OsamesFrameworkCore.Logging;

namespace OsamesFrameworkUI.OfficeWebUiOfficeRibbon
{
    /// <summary>
    /// This control renders as an OfficeWebUI OfficeRibbon with its associated Manager.
    /// It delegates population of specific lists (Ribbon contexts, backstages pages) to
    /// some spcialized classes.
    /// </summary>
    [ToolboxData("<{0}:OfficeWebUiOfficeRibbonContainer runat=server></{0}:OfficeWebUiOfficeRibbonContainer>")]
    public sealed class Container : WebControl, ILoggable
    {
        /// <summary>
        /// Component to fill and render.
        /// </summary>
        private Manager _mManager;
        /// <summary>
        /// Component to fill and render.
        /// </summary>
        private OfficeRibbon _orRibbon;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Container()
        {
            /* aspx equivalent :
             *    <OfficeWebUI:Manager ID="Manager1" runat="server" UITheme="Office2010Silver" />
        <OfficeWebUI:OfficeRibbon ID="OfficeRibbon1" runat="server" ApplicationMenuColor="#851b86" ApplicationMenuText="File" ApplicationMenuType="Backstage" ExtraText="Hello world">

             * */
            // TODO [office ribbon configuration] properties values from application configuration rather than hardcoded.
            _mManager = new Manager
                            {
                                ID = "Manager1",
                                UITheme = Theme.Office2010Silver
                            };
            Controls.Add(_mManager);

            _orRibbon = new OfficeRibbon
                            {
                                ID = "OfficeRibbon1",
                                ApplicationMenuColor = "#851b86",
                                ApplicationMenuText = "File",
                                ApplicationMenuType = ApplicationMenuType.Backstage
                                //ExtraText = "Hello World"
                            };
            Controls.Add(_orRibbon);

            PopulateBackstagePages();
            PopulateRibbonContexts();
        }

        /// <summary>
        /// Asks relevant manager for a list of backstage pages, may perform some checks and add them to internal office ribbon.
        /// </summary>
        private void PopulateBackstagePages()
        {
            BackstagePageManager bpmManager = new BackstagePageManager();
            List<BackstagePage> lstPage = bpmManager.LstBackstagePages;
            // Remark : here we may insert check that there aren't problems such as duplicate ID etc, check study we did...
            // Then set list onto office ribbon.
            _orRibbon.ApplicationMenu.BackstagePages.AddRange(lstPage);
        }

        /// <summary>
        /// Asks relevant manager for a list of ribbon contexts, may perform some checks and add them to internal office ribbon.
        /// </summary>
        private void PopulateRibbonContexts()
        {
            // New manager : load list.
            RibbonContextManager rcmManager = new RibbonContextManager();
            List<RibbonContext> lstRibbon = rcmManager.LstRibbonContext;
            // Remark : here we may insert check that there aren't problems such as duplicate ID etc, check study we did...
            // Then set list onto office ribbon.
            _orRibbon.Contexts.AddRange(lstRibbon);
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="strMessage_">Message</param>
        /// <param name="strMessageDetails_">Optional array of message details. In this case, strMessage_ parameter contains placeholders, each matching a value in strMessageDetails_</param>
        /// <param name="eLogLevel_">Log level</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, object[] strMessageDetails_ = null)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, strMessageDetails_);
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="eLogLevel_">Osames log level</param>
        /// <param name="strMessage_">Message</param>
        /// <param name="ex_">Exception we will log message and stack trace of</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, Exception ex_)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, ex_);
        }
    }
}